package com.ynu.springbootappdemo.controller;

import com.ynu.springbootappdemo.common.annotation.AuthCheck;
import com.ynu.springbootappdemo.common.constant.UserConstant;
import com.ynu.springbootappdemo.common.controller.LogicController;
import com.ynu.springbootappdemo.common.exception.BusinessException;
import com.ynu.springbootappdemo.common.utils.BaseResponse;
import com.ynu.springbootappdemo.common.utils.ErrorCode;
import com.ynu.springbootappdemo.common.utils.ResultUtils;
import com.ynu.springbootappdemo.common.utils.TokenUtils;
import com.ynu.springbootappdemo.dao.IUserDao;
import com.ynu.springbootappdemo.model.dto.User.LoginDTO;
import com.ynu.springbootappdemo.model.entity.User;
import com.ynu.springbootappdemo.model.vo.TokenVo;
import com.ynu.springbootappdemo.model.vo.UserVo;
import com.ynu.springbootappdemo.model.dto.User.RegisterDTO;
import com.ynu.springbootappdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Objects;

@Api(tags = "登陆注册等权限认证控制器")
@RestController
@RequestMapping("/auth")
public class AuthController extends LogicController<UserService, IUserDao, User,Long> {
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private TokenUtils tokenUtils;
    public AuthController(@Autowired UserService ls) {
        super(ls);
    }


    @ApiOperation("用户登录")
    @PostMapping("/login")
    @Transactional
    public BaseResponse<TokenVo> login(@ApiParam("登录信息") @RequestBody LoginDTO loginDTO){
        String pwd = DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes());
        return  ResultUtils.success(getService().login(loginDTO.getUserPhone(),pwd));
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    @Transactional
    public BaseResponse<UserVo> register(@ApiParam("用户注册信息") RegisterDTO user){
        User registerUser = getService().register(user);
        if (Objects.isNull(registerUser)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVo UserVo = modelMapper.map(registerUser, UserVo.class);
        return  ResultUtils.success(UserVo);
    }

    @ApiOperation("获取当前用户")
    @GetMapping("/getLoginUser")
    @AuthCheck(mustRole = UserConstant.DEFAULT_ROLE)
    @Transactional
    public BaseResponse<UserVo> getLoginUser(HttpServletRequest httpServletRequest){
        User loginUser = getService().getLoginUser(httpServletRequest);
        if (Objects.isNull(loginUser)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVo UserVo = modelMapper.map(loginUser, UserVo.class);
        return  ResultUtils.success(UserVo);
    }

    @ApiOperation("通过refreshToken换新令牌")
    @GetMapping("/refresh")
    public BaseResponse<TokenVo> refresh(HttpServletRequest httpServletRequest){
        return  ResultUtils.success(tokenUtils.updateAccessToken(httpServletRequest));
    }

}
